package com.maxlen.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.constant.MemberConstant;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.member.dao.MemberLevelDao;
import com.maxlen.member.entity.MemberLevelEntity;
import com.maxlen.member.entity.vo.MemberLevelVO;
import com.maxlen.member.service.MemberLevelService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;


@Service("memberLevelService")
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelDao, MemberLevelEntity> implements MemberLevelService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberLevelEntity> page = this.page(
                new Query<MemberLevelEntity>().getPage(params),
                new QueryWrapper<MemberLevelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(MemberLevelVO memberLevel) {
        if(ObjectUtils.isEmpty(memberLevel)){
            return;
        }
        MemberLevelEntity entity = CopyUtils.copy(memberLevel, MemberLevelEntity.class);
        baseMapper.insert(entity);
    }

    @Override
    public void updateDetail(MemberLevelVO memberLevel) {
        if(ObjectUtils.isEmpty(memberLevel)){
            return;
        }
        MemberLevelEntity entity = CopyUtils.copy(memberLevel, MemberLevelEntity.class);
        baseMapper.updateById(entity);
    }

    @Override
    public Long getDefaultStatus() {

        LambdaQueryWrapper<MemberLevelEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(MemberLevelEntity::getId);
        wrapper.eq(MemberLevelEntity::getDefaultStatus, MemberConstant.DEFAULT_STATUS);

        MemberLevelEntity memberLevelEntity = baseMapper.selectOne(wrapper);
        return memberLevelEntity.getId();

    }

}
